package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequirePermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Method;
import java.util.*;

@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;
    @Autowired
    private ApplicationContext ctx;
    @RequestMapping("/load")
    @ResponseBody
    @RequirePermission(name = "权限加载", expression = "permission:load") // 自定义注解
    public JsonResult load() {
        try {
            // 获取控制器,将控制器中处理方法转换为权限表中的一条条数据
            Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
            // 通过map.values方法拿到所有的控制器对象
            Collection<Object> controllers = map.values();
            // 创建集合用来装 权限对象
            Set<Permission> permissions = new LinkedHashSet<>();
            // 查询所有的权限数据
            List<Permission> allPermission = permissionService.listAll();
            // 获取控制器处理方法
            for (Object controller : controllers) {
                // 拿到所有的方法
                Method[] methods = controller.getClass().getDeclaredMethods();
                for (Method method : methods) {
                    // 通过method对象拿到注解方法,再拿到注解 => 权限名+权限表达式
                    RequirePermission annotation = method.getAnnotation(RequirePermission.class);
                    if (annotation != null) {
                        // 创建权限对象
                        Permission permission = new Permission();
                        permission.setName(annotation.name());
                        permission.setExpression(annotation.expression());
                        // 若之前库里没有permission,就添加  => Permission实体类要重写equals方法
                        if (!allPermission.contains(permission)) {
                            // 将对象装进集合中
                            permissions.add(permission);
                        }
                    }
                }
            }
            // 当没有数据时,会导致SQL错误
            if (permissions.size() > 0) {
                // 业务对象调用方法
                permissionService.batchSave(permissions);
            }
            // Json结果对象
            return new JsonResult(true, "加载成功"); // JsonResult需要提供构造方法
        } catch (Exception e) {
            return new JsonResult(false, "加载失败");
        }

    }

    @RequestMapping("/list")
    @RequirePermission(name = "权限查询", expression = "permission:list")
    public String list(Model model, QueryObject qo) {
        model.addAttribute("pageInfo", permissionService.query(qo));
        return "permission/list";
    }
}
